package com.yidu.DatelessManagement.mapper;

import com.yidu.DatelessManagement.util.*;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 库存统计
 * @author :yimeng
 * @date :2023/2/1  11:25
 * @description:some description
 */
@Mapper
public interface InventoryStatisticsMapper {
    //查询证券信息库存
    @Select("SELECT ss.securityId,ss.quantity AS quantity ,ss.unitCost,ss.money FROM (SELECT * FROM securityStock WHERE DATE = #{time} and fundId=#{fundId}) ss \n" +
            "UNION all\n" +
            "SELECT dd.securityId, dd.dealNumber as quantity, dealPrice as unitCost,endMoney as money FROM (select * from  dealData  WHERE dealTime = #{dealTime} and fundId=#{fundId}) dd \n")
    List<SecurityDael> selectSecurityStock(@Param("time") String time,@Param("dealTime") String dealTime, @Param("fundId") String fundId);

    @Delete("DELETE FROM securityStock where securityId=#{securityId} and fundId=#{fundId}")
    int deleteSecurityStock(@Param("securityId") String securityId,@Param("fundId") String fundId);


    @Select("SELECT max(securityStockId) FROM securityStock where securityId=#{securityId} and fundId=#{fundId}")
    String selectSecurityDealMaxId(@Param("securityId") String securityId,@Param("fundId") String fundId);

    //查询现金信息库存
    @Select("SELECT ca.cashId,ca.cashStockId,ca.money FROM (SELECT * FROM cashStock WHERE DATE=#{time} AND fundId = #{fundId}) ca\n" +
            "UNION ALL\n" +
            "SELECT fa.cashId,fa.fundsAllotId as cashStockId,fa.allotMoney as money FROM (SELECT * FROM fundsallot WHERE businessTime=#{dealTime} AND fundId = #{fundId}) fa")
    List<CashStockDael> selectCashStock(@Param("time") String time, @Param("dealTime") String dealTime, @Param("fundId") String fundId);

    @Delete("delete from cashStock where cashStockId = #{cashStockId} and fundId = #{fundId}")
    int deleteCashStock(@Param("cashStockId") String cashStockId,@Param("fundId") String fundId);

    @Select("select max(cashStockId) from cashStock where cashId = #{cashId} and fundId = #{fundId}")
    String selectCashStockDealMaxId(@Param("cashId")String cashId,@Param("fundId")String fundId);



    //查询TA信息库存
    @Select("SELECT ts.securityAccrualStockId,ts.money,ts.quantity FROM (SELECT * FROM tastock WHERE DATE = #{time} AND fundId = #{fundId}) ts\n" +
            "UNION ALL\n" +
            "SELECT t.taId AS securityAccrualStockId,t.money,t.quantity FROM (SELECT * FROM TA WHERE dealTime = #{dealTime} AND fundId = #{fundId}) t\n")
    List<TaStockDael> selectTa(@Param("time")String time, @Param("dealTime")String dealTime, @Param("fundId")String fundId);

    @Delete("delete from tastock where securityAccrualStockId=#{securityAccrualStockId} and fundId = #{fundId}")
    int deleteTaStock(@Param("securityAccrualStockId")String securityAccrualStockId,@Param("fundId")String fundId);

    @Select("select max(securityAccrualStockId) from tastock where fundId = #{fundId} and securityAccrualStockId=#{securityAccrualStockId}")
    String selectTaStockMaxId(@Param("fundId")String fundId,@Param("securityAccrualStockId")String securityAccrualStockId);


    //证券应收应付库存
    @Select("SELECT sas.securityAccrualStockId AS securityAccrualStockId,sas.securityId,sas.money,sas.businessState FROM \n" +
            "(SELECT * FROM securityAccrualStock WHERE DATE=#{time} AND fundId = #{fundId}) sas\n" +
            "UNION ALL\n" +
            "SELECT sa.securityAccruedId AS securityAccrualStockId,sa.securityId,sa.money,sa.fundFlow AS businessState FROM \n" +
            "(SELECT * FROM  securityAccrued WHERE businessTime = #{dealTime} AND fundId = #{fundId}) sa")
    List<SecurityAccrualStockDael> selectSecurityAccrualStock(@Param("time")String time,@Param("dealTime")String dealTime,@Param("fundId")String fundId);

    @Delete("delete from securityAccrualStock where securityAccrualStockId=#{securityAccrualStockId} and fundId = #{fundId}")
    int deleteSecurityAccrualStock(@Param("securityAccrualStockId")String securityAccrualStockId,@Param("fundId")String fundId);

    @Select("select max(securityAccrualStockId) from securityAccrualStock where fundId = #{fundId} and securityId=#{securityId}")
    String selectSecurityAccrualStockMaxId(@Param("fundId")String fundId,@Param("securityId")String securityId);



    //现金应收应付库存
    @Select("SELECT cas.cashId,cas.money,cas.cashAccrualStockId FROM (SELECT * FROM cashAccrualStock WHERE DATE = #{time}) cas\n" +
            "UNION ALL\n" +
            "SELECT ca.cashId,ca.money,ca.cashAccrued as cashAccrualStockId FROM (SELECT * FROM cashAccrued WHERE businessTime = #{dealTime} AND fundId = #{fundId}) ca")
    List<CashAccrualStockDael> selectCashAccrualStock(@Param("time") String time,@Param("dealTime") String dealTime,@Param("fundId") String fundId);

    @Delete("delete from cashAccrualStock where cashAccrualStockId=#{cashAccrualStockId} and date = #{time}")
    int deleteCashAccrualStock(@Param("cashAccrualStockId")String cashAccrualStockId,@Param("time")String time);

    @Select("select max(cashAccrualStockId) from cashAccrualStock where cashId=#{cashId}")
    String selectCashAccrualStockMaxId(@Param("cashId")String cashId);




}
